<?php
class TreeHelper extends Helper
{
	var $helpers = array('Session');
  var $tab = "  ";
  var $logged = false;
  var $uid=false;
  var $closed=false;
  var $voted=true;
  var $uservotes=false;
	var $lastseen=false;
	var $thread=false;
	var $tz=0;
	/*var $tr = false;*/

  function showComments($data, $uservotes, $closed, $voted)
  {
  if($this->Session->check('User')) {
		$this->logged=true;
		$this->uid=$this->Session->read('User.id');
		$this->thread=$this->Session->read('User.thread') ? true:false;
		$this->tz=$this->Session->read('User.tz');
	}
  if($closed) {
		$this->closed=true;
	}
  if(!$voted) {
		$this->voted=false;
	}
	$this->uservotes=$uservotes;
  $output = $this->list_comments($data, 0);

  return $this->output($output);
  }

  function list_comments($data, $level)
  {
    $tabs = "\n" . str_repeat($this->tab, $level * 2);
    $li_tabs = $tabs . $this->tab;

    $output = $tabs. "<ul>";
		if($this->thread) {
        foreach ($data as $key=>$val)
        {
    	  	$output .= $li_tabs . $this->showcomment($val);
          if(isset($val['children'][0]))
          {
            $output .= $this->list_comments($val['children'], $level+1);
            $output .= $li_tabs . "</li>";
          }
          else
          {
            $output .= "</li>";
          }
        }
		} // if thread
		else {
      foreach ($data as $key=>$val)
        {
    	  	$output .= $this->showcomment($val);
          $output .= "</li>";
        }
		}
    $output .= $tabs . "</ul>";

    return $output;
  }

  function showcomment($d) {
	if ($d['Comment']['del']) {
		$t="reply deleted";
	} else if ($d[0]['text']=="-" && !$d['Comment']['del']) {
		$t="<a href=\"#\" onclick=\"loadcom({$d['Comment']['id']}); return false;\">show comment</a>";
	} else {
		$t=br($d[0]['text']);
	}
	$reply=""; $vote=""; $isvoted=false; $liclass="";
	if($d['Comment']['vote']>0) {
	$vclass="valueplus";
	$d['Comment']['vote']='+'.$d['Comment']['vote'];
	}
	else $vclass="valueminus";
	if($this->logged) {
		if (is_array($this->uservotes))
			if (in_array($d['Comment']['id'], $this->uservotes)) $isvoted=true;

		if(!$this->closed && !$d['Comment']['del']) {
		$reply=<<<EOD
<a class="replink" href="/topic/{$d['Comment']['post_id']}.html?reply={$d['Comment']['id']}">Reply</a>
<div id="CommentForm{$d['Comment']['post_id']}-{$d['Comment']['id']}" style="display:none"></div>
EOD;
		}
		if ($this->voted) {
    if($isvoted || $d['Comment']['del'] || ($d['Comment']['user_id']==$this->uid)) {
		$vote=<<<EOD
<span class="{$vclass}" id="vote{$d['Comment']['id']}">{$d['Comment']['vote']}</span>
EOD;
	}
	else {
    	$vote=<<<EOD
<span id="vote{$d['Comment']['id']}">
<span class="{$vclass}">{$d['Comment']['vote']}</span>
<a class="vplus" href="/votes/add/{$d['Comment']['id']}/1/"><img src="/img/i-plus.gif" alt="+1" width="16" height="16"></a>
<a class="vminus" href="/votes/add/{$d['Comment']['id']}/0/"><img src="/img/i-minus.gif" alt="-1" width="16" height="16"></a>
</span>
EOD;
	} // voting
	} // voted
	} // is logged
	else {
		if ($this->voted) {
		$vote=<<<EOD
<span class="{$vclass}" id="vote{$d['Comment']['id']}">{$d['Comment']['vote']}</span>
EOD;
		} // voted
	}

	$cdate=date("d.m.y H:i",strtotime($d['Comment']['created'])+(3600 * $this->tz));
	if($d['Comment']['user_id']==$this->uid) $liclass=' class="mycomment"';
  	return <<<EOD
<li{$liclass}>
<a class="userlink" href="/user/{$d['User']['login']}/" title="{$d['User']['name']}">{$d['User']['login']}</a> <a class="comlink" name="c{$d['Comment']['id']}" href="#c{$d['Comment']['id']}">{$cdate}</a>
{$vote}
<div id="t{$d['Comment']['id']}" class="commenttext">{$t}</div>
{$reply}
EOD;
  }

  function avatar($l,$a) {
			$size='width="24" height="24"';
		if($a==0) {
			$text='<img class="avsm" src="/img/1s.gif" '.$size.'>';
		} else {
			$text='<img class="avsm" src="/pic/'.$l.'.jpg" '.$size.'>';
		}
		return $text;
	}

  function showCats($data)
  {
    if($this->Session->check('User')) {
			$this->logged=true;
			$this->lastseen=$this->Session->read('User.lastseen');
		}

    $output = $this->list_cats($data, 0);

    return $this->output($output);
  }

  function list_cats($data, $level)
  {
    $tabs = "\n" . str_repeat($this->tab, $level * 2);
    $li_tabs = $tabs . $this->tab;

    $output = $tabs. "<ul>";
    foreach ($data as $key=>$val)
    {
      //$output .= $li_tabs . "<li>".$val[$modelName][$fieldName];
		  $output .= $li_tabs . $this->showcat($val);
      if(isset($val['children'][0]))
      {
        $output .= $this->list_cats($val['children'], $level+1);
        $output .= $li_tabs . "</li>";
      }
      else
      {
        $output .= "</li>";
      }
    }
    $output .= $tabs . "</ul>";

    return $output;
  }

  function showcat($d) {
		$newt='';
    if($this->logged && $d['Category']['count'] && strtotime($d['Category']['modified'])-strtotime($this->lastseen)>0)
			$newt='<span class="new">+</span>';
  	return <<<EOD
<li><a href="/group/{$d['Category']['name']}/">{$d['Category']['description']}</a> ({$d['Category']['count']}{$newt})
EOD;
  }

}
?>